//
//  XHStatusOriginalView.swift
//  gu-新浪微博
//
//  Created by xiaohui on 16/6/28.
//  Copyright © 2016年 gusheghui. All rights reserved.
//

import UIKit

import SnapKit
class XHStatusOriginalView: UIView {

    var oraginalViewConstraint: Constraint?
    
    
    
    var status:XHStatus?{
        didSet{
           nameLabel.text = status?.user?.name
            status?.user?.profile_image_url
           headImageView.sd_setImageWithURL(NSURL(string: status?.user?.profile_image_url ?? ""), placeholderImage: UIImage(named: "avatar_default_big"))
            contetLable.text = status?.text
            //加载认证图片
            vipImageView.image = status?.user?.verifiedImage
            //加载vip等级
            expiredImageView.image = status?.user?.mbrankImage
            
            //来源iphoe6还是xxx
//            print(status?.source)
            sourceLabel.attributedText =  getSourceStr(status?.source)
            timeLabel.text = "2388"
            
            
            
            //卸载约束
            oraginalViewConstraint?.uninstall()
            
            if status?.pic_urls?.count > 0{//显示图片
                pictureView.hidden = false
                //赋值
                pictureView.pic_urls = status?.pic_urls
                self.snp_updateConstraints { (make) -> Void in
                    oraginalViewConstraint = make.bottom.equalTo(pictureView).offset(Margin).constraint
                }
                
            }else{//不显示图片
                pictureView.hidden = true
                self.snp_updateConstraints { (make) -> Void in
                    oraginalViewConstraint = make.bottom.equalTo(contetLable.snp_bottom).offset(Margin).constraint
                }
            }           
        }
    }

    
    func getSourceStr(source: String?) -> NSMutableAttributedString{
        guard let s = source where s.containsString("\">")  else {
            let r = "来自 月球"
            return getSourceAttr(r)
        }
        // 字符串的截取
        let start = s.rangeOfString("\">")!
        let end = s.rangeOfString("</a")!
        // 字符串截取
        let result = s.substringWithRange(start.endIndex..<end.startIndex)
        let r = "来自 \(result)"
        return getSourceAttr(r)
    }
    // 通过字符串生成富文本
    func getSourceAttr(result: String) -> NSMutableAttributedString{
        // 实例化一个富文本
        let attr = NSMutableAttributedString(string: result)
        let range = (result as NSString).rangeOfString("来自")
        attr.addAttributes([NSForegroundColorAttributeName: ThemeColor], range: range)
        return attr
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
 
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //设置cell上方的View
    private func setupUI(){
      //添加控件
//        backgroundColor = RandomColor()
        backgroundColor = UIColor.whiteColor()
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(expiredImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(vipImageView)
        addSubview(contetLable)
        addSubview(pictureView)
        //设置约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.left.top.equalTo(self).offset(Margin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(headImageView.snp_right).offset(Margin)
           
            make.top.equalTo(headImageView)
        }
        expiredImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(Margin)
            make.centerY.equalTo(nameLabel)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp_right).offset(Margin)
        }
        vipImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(headImageView.snp_bottom)
            make.centerX.equalTo(headImageView.snp_right)
        }
        contetLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(headImageView)
            make.top.equalTo(headImageView.snp_bottom).offset(Margin)
        }
        //设置collectionView的约束
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contetLable.snp_bottom).offset(Margin)
            make.left.equalTo(contetLable)
        }
        //原创微博（内容+图）
        self.snp_makeConstraints { (make) -> Void in
            oraginalViewConstraint = make.bottom.equalTo(pictureView).offset(Margin).constraint
        }
        
    }
    //懒加载控件
    //头像
    private lazy var headImageView:UIImageView = UIImageView(imgName: "avatar_default_big")
    //昵称
    private lazy var nameLabel : UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15)
    //vip等级
    private lazy var expiredImageView : UIImageView = UIImageView(imgName: "common_icon_membership_expired")
    //发布的时间
    private lazy var timeLabel:UILabel = UILabel(textColor: ThemeColor, textFont: 10)
    //来自客户端还是手机类型
    private lazy var sourceLabel:UILabel = UILabel(textColor: UIColor.grayColor(), textFont: 10)
    //头像右下角图片
    private lazy var vipImageView:UIImageView = UIImageView(imgName: "avatar_vgirl")
    //微博内容
    private lazy var contetLable:UILabel = UILabel(textColor: UIColor.blackColor(), textFont: 15, maxWdith: SCREENW - 2*Margin)
    //原创微博里面的图片 collectionView
    private lazy var pictureView:XHPictureView = XHPictureView()
   
}
